//
//  FSBaseTableViewCell.m
//  fanshu
//
//  Created by jklin on 16/3/28.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSBaseTableViewCell.h"

#import "FSBasicRowItem.h"
#import "FSArrowRowItem.h"
#import "FSSwitchRowItem.h"

@implementation FSBaseTableViewCell
//xib加载的时候添加子控件用
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];


}
-(instancetype)initBaseCellWithTableView:(UITableView *)tableView Style:(UITableViewCellStyle)style{
    self = [super init];
    static NSString *ID = @"baseCell";
    self = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self == nil) {
        self = [[FSBaseTableViewCell alloc]initWithStyle:style reuseIdentifier:ID];
    }

    return self;


}
//初始化cell
+(instancetype)BaseCellWithTableView:(UITableView *)tableView Style:(UITableViewCellStyle)style{
    static NSString *ID = @"baseCell";
    FSBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
         cell = [[FSBaseTableViewCell alloc]initWithStyle:style reuseIdentifier:ID];
    }

    return cell;
}

-(void)setCellWithRowItem:(FSBasicRowItem *)rowItem{
//设置数据
    self.rowItem = rowItem;
    [self setCellSource:rowItem];
//设置格式
    [self setCellAccessoryView:rowItem];

}

-(void)setCellSource:(FSBasicRowItem *)rowItem{
    //设置头像尺寸
    self.imageView.image = rowItem.image;
    if (rowItem.isLoginItem) {
        self.imageView.image = [self circleImage:rowItem.image];
    }
    self.textLabel.text = rowItem.title;
    self.detailTextLabel.text = rowItem.detailTitle;
    NSLog(@"%@,%@",rowItem.detailTitle,rowItem.title);

}
-(UIImage *)circleImage:(UIImage *)image{
    // 裁剪图片: 图形上下文
    // 1.开启图形上下文
    // scale:比例因素 点:像素比例 0:自动识别比例因素
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    // 2.描述圆形裁剪路径
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 3.设置为裁剪区域
    [circlePath addClip];
    // 4.画图片
    [image drawAtPoint:CGPointZero];
    // 5.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 6.关闭上下文
    UIGraphicsEndImageContext();
    return newImage;
}
-(void)setCellAccessoryView:(FSBasicRowItem *)rowItem{
    
    if ([rowItem isKindOfClass:[FSArrowRowItem class]]) {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"SVWebViewControllerNext"]];
        imageView.bounds = CGRectMake(0, 0, 10, 15);
    
        self.accessoryView = imageView;
    }else if([rowItem isKindOfClass:[FSSwitchRowItem class]]){
        UISwitch *switchBtn = [[UISwitch alloc]init];
        switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:rowItem.title];
        self.accessoryView = switchBtn;
    }else{
        self.accessoryView = nil;
    }

}

//定义分割线
-(void)setFrame:(CGRect)frame{
    frame.size.height -= 1;
    [super setFrame:frame];
}
//修改登录cell的图标大小
-(void)layoutSubviews{
    [super layoutSubviews];
    if (self.rowItem.isLoginItem) {
        self.imageView.bounds = CGRectMake(0, 0, 60, 60);
    }

}













@end
